import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/driver_info_entity.dart';
import 'package:nyh_motorcade_app/pages/card_manage/card_details/card_details_controller.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:nyh_motorcade_app/widgets/flexible_icon_text.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/default_empty_widget.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'package:nyh_motorcade_app/widgets/text_with_icon.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CardDetailsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String tag = DateTime.now().millisecondsSinceEpoch.toString();
    final controller = Get.put(CardDetailsController(), tag: tag);
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(
        context: context,
        title: "卡详情",
        actions: [
          IconButton(
            onPressed: () {
              controller.goBackHomePage();
            },
            icon: Image.asset("images/ic_back_home.png"),
          ),
        ],
      ),
      body: GetBuilder<CardDetailsController>(
        tag: tag,
        builder: (controller) {
          return MultiStateWidget(
            child: SmartRefresher(
              controller: controller.refreshController,
              onRefresh: controller.onRefresh,
              child: CustomScrollView(
                physics: BouncingScrollPhysics(),
                slivers: [
                  buildHeaderWidget(controller),
                  buildListWidget(controller),
                ],
              ),
            ),
            state: controller.multiState,
            onRetry: () {
              controller.onRetry();
            },
          );
        },
      ),
      floatingActionButton: GestureDetector(
        onTap: () {
          controller.goToBindDriverPage();
        },
        child: Image.asset("images/ic_bind_circle.png"),
      ),
    );
  }

  Widget buildHeaderWidget(CardDetailsController controller) {
    return SliverToBoxAdapter(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
            decoration: BoxDecoration(
              color: Colours.color_white,
              borderRadius: BorderRadius.circular(10.w),
              boxShadow: [
                BoxShadow(
                  color: Colours.color_0f112e_95,
                  offset: Offset(0.0, 2.w),
                  blurRadius: 5.w,
                ),
              ],
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 13.w, horizontal: 16.w),
                  child: Column(
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Image.asset(
                            "images/ic_card_circle.png",
                            fit: BoxFit.cover,
                            height: 42.w,
                            width: 42.w,
                          ),
                          SizedBox(
                            width: 16.w,
                          ),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Expanded(
                                      child: Row(
                                        children: [
                                          Flexible(
                                            child: Text(
                                              controller.cardDetailsEntity?.name ?? "",
                                              style: TextStyle(
                                                color: Colours.color_text_theme,
                                                fontSize: 16.sp,
                                                fontWeight: FontWeight.bold,
                                              ),
                                              maxLines: 1,
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                          ),
                                          Visibility(
                                            visible: (controller.cardDetailsEntity?.cardType ?? "").isNotEmpty,
                                            child: Container(
                                              margin: EdgeInsets.only(left: 8.w),
                                              padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 1.w),
                                              decoration: BoxDecoration(
                                                color: (controller.cardDetailsEntity?.cardType ?? "") == Constant.OWN_CARD ? Colours.color_34c759 : Colours.color_theme,
                                                borderRadius: BorderRadius.circular(5.w),
                                                border: Border.all(
                                                  width: 1.w,
                                                  color: (controller.cardDetailsEntity?.cardType ?? "") == Constant.OWN_CARD ? Colours.color_34c759 : Colours.color_theme,
                                                ),
                                              ),
                                              child: Text(
                                                controller.cardDetailsEntity?.cardTypeName ?? "",
                                                style: TextStyle(
                                                  color: Colours.color_white,
                                                  fontSize: 10.sp,
                                                ),
                                              ),
                                            ),
                                          ),
                                          Visibility(
                                            visible: (controller.cardDetailsEntity?.energyTypeName ?? "").isNotEmpty,
                                            child: Container(
                                              margin: EdgeInsets.only(left: 8.w),
                                              padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 1.w),
                                              decoration: BoxDecoration(
                                                color: Colours.color_white,
                                                borderRadius: BorderRadius.circular(5.w),
                                                border: Border.all(width: 1.w, color: Colours.color_6f7082),
                                              ),
                                              child: Text(
                                                controller.cardDetailsEntity?.energyTypeName ?? "",
                                                style: TextStyle(
                                                  color: Colours.color_6f7082,
                                                  fontSize: 10.sp,
                                                ),
                                              ),
                                            ),
                                          ),
                                          Visibility(
                                            visible: controller.cardDetailsEntity?.status == 0,
                                            child: Container(
                                              margin: EdgeInsets.only(left: 8.w),
                                              padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 1.w),
                                              decoration: BoxDecoration(
                                                color: Colours.color_cfcfd5,
                                                borderRadius: BorderRadius.circular(5.w),
                                                border: Border.all(width: 1.w, color: Colours.color_cfcfd5),
                                              ),
                                              child: Text(
                                                "已禁用",
                                                style: TextStyle(
                                                  color: Colours.color_white,
                                                  fontSize: 10.sp,
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    SizedBox(
                                      width: 10.w,
                                    ),
                                    GestureDetector(
                                      behavior: HitTestBehavior.opaque,
                                      onTap: () {
                                        controller.goToEditCardInfoPage();
                                      },
                                      child: Padding(
                                        padding: EdgeInsets.only(left: 5.w, top: 5.w, bottom: 5.w),
                                        child: Image.asset("images/ic_edit.png"),
                                      ),
                                    ),
                                  ],
                                ),
                                SizedBox(
                                  height: 5.w,
                                ),
                                Text(
                                  controller.cardDetailsEntity?.cardNumber ?? "",
                                  style: TextStyle(
                                    color: Colours.color_cfcfd5,
                                    fontSize: 12.sp,
                                  ),
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 5.w,
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: Row(
                              children: [
                                Text(
                                  "余额(元)：",
                                  style: TextStyle(
                                    color: Colours.color_6f7082,
                                    fontSize: 12.sp,
                                  ),
                                ),
                                Expanded(
                                  child: Text(
                                    FormatUtils.formatMoney(controller.cardDetailsEntity?.cardBalance),
                                    style: TextStyle(
                                      color: Colours.color_6f7082,
                                      fontSize: 14.sp,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                          SizedBox(
                            width: 8.w,
                          ),
                          Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              TextButton(
                                onPressed: () {
                                  controller.goToCardCashFlowPage(controller.cardDetailsEntity?.id);
                                },
                                child: Text(
                                  "资金流水",
                                  style: TextStyle(
                                    color: Colours.color_6f7082,
                                    fontSize: 14.sp,
                                  ),
                                ),
                                style: ButtonStyle(
                                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                                  minimumSize: MaterialStateProperty.all(Size(80.w, 30.w)),
                                  padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                                  side: MaterialStateProperty.all(BorderSide(width: 1.w, color: Colours.color_e2e2e5)),
                                  shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.w))),
                                ),
                              ),
                              SizedBox(
                                width: controller.permissionTransferCard ? 8.w : 0,
                              ),
                              Visibility(
                                visible: controller.permissionTransferCard,
                                child: TextButton(
                                  onPressed: () {
                                    controller.goToRapidTransferPage(
                                      controller.cardDetailsEntity?.id,
                                      controller.cardDetailsEntity?.name,
                                      controller.cardDetailsEntity?.cardBalance,
                                    );
                                  },
                                  child: Text(
                                    "划拨",
                                    style: TextStyle(
                                      color: Colours.color_theme,
                                      fontSize: 14.sp,
                                    ),
                                  ),
                                  style: ButtonStyle(
                                    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                                    minimumSize: MaterialStateProperty.all(Size(80.w, 30.w)),
                                    padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                                    side: MaterialStateProperty.all(BorderSide(width: 1.w, color: Colours.color_theme)),
                                    shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.w))),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                Visibility(
                  visible: (controller.cardDetailsEntity?.remark ?? "").isNotEmpty,
                  child: Divider(
                    color: Colours.color_background,
                    height: 1.w,
                    thickness: 1.w,
                  ),
                ),
                Visibility(
                  visible: (controller.cardDetailsEntity?.remark ?? "").isNotEmpty,
                  child: Padding(
                    padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 16.w),
                    child: Text(
                      controller.cardDetailsEntity?.remark ?? "",
                      style: TextStyle(
                        color: Colours.color_cfcfd5,
                        fontSize: 12.sp,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                ),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.only(left: 16.w, bottom: 12.w),
            child: Text(
              "司机信息(${controller.driverInfoList.length})",
              style: TextStyle(
                color: Colours.color_text_theme,
                fontSize: 14.sp,
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget buildListWidget(CardDetailsController controller) {
    return controller.driverInfoList.isNotEmpty
        ? SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                return buildItemWidget(controller, index);
              },
              childCount: controller.driverInfoList.length,
            ),
          )
        : SliverFillRemaining(
            child: DefaultEmptyWidget(),
          );
  }

  Widget buildItemWidget(CardDetailsController controller, int index) {
    DriverInfoItem entity = controller.driverInfoList[index];
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {
              controller.goToDriverDetailsPage(entity.id);
            },
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 13.w, horizontal: 16.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Text(
                                entity.name ?? "",
                                style: TextStyle(
                                  color: Colours.color_text_theme,
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                              Visibility(
                                visible: (entity.belongingType ?? "").isNotEmpty,
                                child: Container(
                                  margin: EdgeInsets.only(left: 8.w),
                                  padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 1.w),
                                  decoration: BoxDecoration(
                                    color: (entity.belongingType ?? "") == Constant.OWN_DRIVER ? Colours.color_34c759 : Colours.color_theme,
                                    borderRadius: BorderRadius.circular(5.w),
                                    border: Border.all(
                                      width: 1.w,
                                      color: (entity.belongingType ?? "") == Constant.OWN_DRIVER ? Colours.color_34c759 : Colours.color_theme,
                                    ),
                                  ),
                                  child: Text(
                                    entity.belongingTypeName ?? "",
                                    style: TextStyle(
                                      color: Colours.color_white,
                                      fontSize: 10.sp,
                                    ),
                                  ),
                                ),
                              ),
                              Visibility(
                                visible: entity.ifCardModel == 1,
                                child: Container(
                                  margin: EdgeInsets.only(left: 8.w),
                                  padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 1.w),
                                  decoration: BoxDecoration(
                                    color: Colours.color_white,
                                    borderRadius: BorderRadius.circular(5.w),
                                    border: Border.all(width: 1.w, color: Colours.color_6f7082),
                                  ),
                                  child: Text(
                                    "卡账户",
                                    style: TextStyle(
                                      color: Colours.color_6f7082,
                                      fontSize: 10.sp,
                                    ),
                                  ),
                                ),
                              ),
                              Visibility(
                                visible: (entity.status ?? 0) == 0,
                                child: Container(
                                  margin: EdgeInsets.only(left: 8.w),
                                  padding: EdgeInsets.only(left: 5.w, right: 5.w, bottom: 1.w),
                                  decoration: BoxDecoration(
                                    color: Colours.color_cfcfd5,
                                    borderRadius: BorderRadius.circular(5.w),
                                    border: Border.all(width: 1.w, color: Colours.color_cfcfd5),
                                  ),
                                  child: Text(
                                    "已禁用",
                                    style: TextStyle(
                                      color: Colours.color_white,
                                      fontSize: 10.sp,
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          TextButtonWithIcon(
                            onPressed: () {
                              showUnboundDialog(controller, entity.id);
                            },
                            icon: Image.asset("images/ic_unbound.png"),
                            label: Text(
                              "解绑",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 14.sp,
                              ),
                            ),
                            textIconPadding: 3.w,
                            style: ButtonStyle(
                              tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              minimumSize: MaterialStateProperty.all(Size(0, 0)),
                              padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 5.w,
                      ),
                      Row(
                        children: [
                          TextWithIcon(
                            text: entity.phone ?? "",
                            iconName: "images/ic_phone.png",
                            iconHeight: 14.w,
                            iconWidth: 14.w,
                            iconColor: Colours.color_6f7082,
                            textStyle: TextStyle(color: Colours.color_6f7082, fontSize: 12.sp),
                            textIconPadding: 5.w,
                          ),
                          Visibility(
                            visible: (entity.motorcadeName ?? "").isNotEmpty,
                            child: Text(
                              "  l  ",
                              style: TextStyle(color: Colours.color_e7e7ea, fontSize: 12.sp),
                            ),
                          ),
                          Visibility(
                            visible: (entity.motorcadeName ?? "").isNotEmpty,
                            child: Flexible(
                              child: TextWithIcon(
                                text: entity.motorcadeName ?? "",
                                iconName: "images/ic_motorcade.png",
                                iconHeight: 14.w,
                                iconWidth: 14.w,
                                iconColor: Colours.color_6f7082,
                                textStyle: TextStyle(color: Colours.color_6f7082, fontSize: 12.sp),
                                textIconPadding: 5.w,
                              ),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: (entity.subCompanyList ?? []).isNotEmpty ? 5.w : 0,
                      ),
                      FlexibleIconText(
                        textList: entity.subCompanyList ?? [],
                        icon: Image.asset(
                          "images/ic_company.png",
                          height: 14.w,
                          width: 14.w,
                          color: Colours.color_6f7082,
                        ),
                        iconTextPadding: 5.w,
                      ),
                    ],
                  ),
                ),
                Visibility(
                  visible: (entity.remake ?? "").isNotEmpty,
                  child: Divider(
                    color: Colours.color_background,
                    height: 1.w,
                    thickness: 1.w,
                  ),
                ),
                Visibility(
                  visible: (entity.remake ?? "").isNotEmpty,
                  child: Padding(
                    padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 16.w),
                    child: Text(
                      entity.remake ?? "",
                      style: TextStyle(
                        color: Colours.color_cfcfd5,
                        fontSize: 12.sp,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void showUnboundDialog(CardDetailsController controller, String? id) {
    showCupertinoDialog(
      barrierDismissible: true,
      context: Get.context!,
      builder: (c) {
        return CupertinoAlertDialog(
          content: Text(
            "是否解绑该司机?",
            style: TextStyle(
              color: Colours.color_text_theme,
              fontSize: 14.sp,
            ),
          ),
          actions: [
            CupertinoDialogAction(
              child: Text(
                "取消",
                style: TextStyle(color: Colours.color_6f7082, fontSize: 14.sp),
              ),
              onPressed: () {
                Get.back();
              },
            ),
            CupertinoDialogAction(
              child: Text(
                "确定",
                style: TextStyle(color: Colours.color_theme, fontSize: 14.sp),
              ),
              onPressed: () {
                Get.back();
                controller.unboundDriver(id);
              },
            ),
          ],
        );
      },
    );
  }
}
